﻿<template>
  <my-container v-loading="pageLoading">
    <!--顶部操作-->
    <template #header>
      <el-form class="ad-form-query" :inline="true" @submit.native.prevent>
        <el-form-item>
          <my-search :fields="fields" @click="onSearch" />
        </el-form-item>
        <el-form-item v-if="checkPermission(['api:admin:package:add'])">
          <el-button type="primary" @click="onAdd">新增</el-button>
        </el-form-item>
        <el-form-item v-if="checkPermission(['api:admin:package:delete'])">
          <my-confirm-button :disabled="sels.length === 0" :type="'delete'" :placement="'bottom-end'"
            :loading="deleteLoading" style="margin-left: 0px;" @click="onBatchDelete">
            <template #content>
              <p>确定要批量删除吗？</p>
            </template>
            批量删除
          </my-confirm-button>
        </el-form-item>
      </el-form>
    </template>

    <!--列表-->
    <el-table v-loading="listLoading" :data="pageList" highlight-current-row height="'100%'"
      style="width: 100%;height:100%;" @selection-change="onSelsChange">
           
      <el-table-column type="selection" width="50" />
         
      <el-table-column prop="name" label="名称" width="120" />
           
      <el-table-column prop="carrierOperatorName" label="运营商" width="100" />
           
      <el-table-column prop="carrierOperatorPackageDictionaryName" label="套餐系列" width="120" />
           
      <el-table-column prop="rechargeDictionaryName" label="充值类型" />
           
      <el-table-column prop="dictionaryName" label="套餐类型" />
           
      <el-table-column prop="packageAmount" label="套餐金额" />

      <el-table-column prop="realFlow" label="真量(G)" />
           
      <el-table-column prop="emptyFlow" label="虚量(G)" />

      <el-table-column prop="sort" label="排序">
        <template slot-scope="scope">
          <el-tag type="success">{{
          scope.row.sort
          }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="isShow" label="显示">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isShow ? 'success' : 'danger'">{{
          scope.row.isShow ? "显示" : "不显示"
          }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="isDisable" label="禁用">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isDisable ? 'danger' : 'success'">{{
          scope.row.isDisable ? "禁用" : "启用"
          }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="isHeat" label="热卖">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isHeat ? 'success' : 'danger'">{{
          scope.row.isHeat ? "热卖" : "未热卖"
          }}</el-tag>
        </template>
      </el-table-column>
           
      <el-table-column label="备注" width="120">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p style="width: 120px;">{{ scope.row.remark }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="mini">查看备注</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
           
      <el-table-column prop="creationTime" label="创建时间" :formatter="formatCreatedTime" />
            <el-table-column v-if="checkPermission(['api:admin:package:update', 'api:admin:package:delete'])" label="操作"
        width="280">
                <template #default="{ $index, row }">
                    <el-button v-if="checkPermission(['api:admin:package:update'])" @click="onFictitiousEdit(1, row)"
            type="success">虚量
          </el-button>
                    <el-button v-if="checkPermission(['api:admin:package:update'])" @click="onFictitiousEdit(2, row)"
            type="primary">限速
          </el-button>
                    <el-button v-if="checkPermission(['api:admin:package:update'])" @click="onEdit($index, row)">编辑
          </el-button>
                   
          <my-confirm-button v-if="checkPermission(['api:admin:package:delete'])" type="delete" :loading="row._loading"
            @click="onDelete($index, row)" />
                 
        </template>
              </el-table-column>
    </el-table>

    <!--分页-->
    <template #footer>
      <my-pagination ref="pager" :total="total" :checked-count="sels.length" @get-page="pageList" />
    </template>

    <!-- 虚量配置 -->
    <my-window v-if="checkPermission(['api:admin:package:add'])" title="虚量配置"
      :visible.sync="addFictitiousDialogFormVisible" @close="closeAddLimitVelocityFictitiousForm">
      <el-form ref="addLimitVelocityFictitiousForm" :model="addLimitVelocityFictitiousForm"
        :rules="addLimitVelocityFictitiousFormRules" label-width="80px" label-position="top">
        <el-form-item>
          <el-row v-for="item in addLimitVelocityFictitiousForm.emptyFlowFictitiousJson">
            <el-col :span="5">
              <el-form-item label="起始流量G" prop="begin">
                <el-input-number v-model="item.begin"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col class="line" :span="1"></el-col>
            <el-col :span="5">
              <el-form-item label="结束流量G" prop="end">
                <el-input-number v-model="item.end"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col class="line" :span="1"></el-col>
            <el-col :span="5">
              <el-form-item label="虚量比例" prop="ratio">
                <el-input-number v-model="item.ratio"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col class="line" :span="1"></el-col>
            <el-col :span="3">
              <el-form-item label="使用真量" prop="real">
                {{ item.end - item.begin }}G
              </el-form-item>
            </el-col>
            <el-col class="line" :span="1"></el-col>
            <el-col :span="3">
              <el-form-item label="使用虚量" prop="fictitiou">
                {{ (item.end - item.begin) * item.ratio }}G
              </el-form-item>
            </el-col>
            <el-col :span="3">
              <el-form-item label="操作" prop="operation">
                <el-button @click="onAddEmptyFlowFictitiousJson" type="primary">+</el-button>
                <el-button @click="onDeleteEmptyFlowFictitiousJson(item.sort)" v-show="item.sort != 0" type="danger">-
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click.native="addFictitiousDialogFormVisible = false">取消</el-button>
        <my-confirm-button type="submit" :validate="addLimitVelocityFictitiousFormvalidate"
          :loading="addFictitiousLoading" @click="onAddFictitiousSubmit" />
      </template>
    </my-window>

    <!-- 限速配置 -->
    <my-window v-if="checkPermission(['api:admin:package:add'])" title="限速配置"
      :visible.sync="addLimitVelocityDialogFormVisible" @close="closeAddLimitVelocityFictitiousForm">
      <el-form ref="addLimitVelocityFictitiousForm" :model="addLimitVelocityFictitiousForm"
        :rules="addLimitVelocityFictitiousFormRules" label-width="80px" label-position="top">
        <el-form-item>
          <el-form-item label="限速类型" prop="limitDictionaryId">
            <el-select v-model="addLimitVelocityFictitiousForm.limitDictionaryId" placeholder="请选择限速类型"
              style="width: 100%">
              <el-option v-for="item in networkCardLimitDictionaryList" :key="item.id" :label="item.name"
                :value="item.id" />
            </el-select>
          </el-form-item>

          <el-row v-for="item in addLimitVelocityFictitiousForm.speedFictitiousJson">
            <el-col :span="6">
              <el-form-item label="起始流量G" prop="begin">
                <el-input-number v-model="item.begin"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col class="line" :span="1"></el-col>
            <el-col :span="6">
              <el-form-item label="结束流量G" prop="end">
                <el-input-number v-model="item.end"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col class="line" :span="1"></el-col>
            <el-col :span="6">
              <el-form-item label="限速(M)" prop="limitVelocity">
                <el-input-number v-model="item.limitVelocity"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="操作" prop="operation">
                <el-button @click="onAddLimitVelocityJson" type="primary">+</el-button>
                <el-button @click="onDeleteLimitVelocityJson(item.sort)" v-show="item.sort != 0" type="danger">-
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click.native="addLimitVelocityDialogFormVisible = false">取消</el-button>
        <my-confirm-button type="submit" :validate="addLimitVelocityFictitiousFormvalidate"
          :loading="addLimitVelocityLoading" @click="onAddLimitVelocitySubmit" />
      </template>
    </my-window>

    <!-- 添加 -->
    <my-window v-if="checkPermission(['api:admin:package:add'])" title="添加" :visible.sync="addDialogFormVisible"
      @close="closeAddForm">
      <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="130px" :inline="false">

        <el-form-item label="名称" prop="name">
          <el-input v-model="addForm.name" auto-complete="off" />
        </el-form-item>

        <el-form-item label="运营商" prop="carrierOperatorId">
          <el-select v-model="addForm.carrierOperatorId" placeholder="请选择运营商" style="width: 100%">
            <el-option v-for="item in carrierOperatorList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="套餐系列" prop="carrierOperatorPackageDictionaryId">
          <el-select v-model="addForm.carrierOperatorPackageDictionaryId" placeholder="请选择套餐系列" style="width: 100%">
            <el-option v-for="item in carrierOperatorPackageDictionaryList" :key="item.id" :label="item.name"
              :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="充值类型" prop="rechargeDictionaryId">
          <el-select v-model="addForm.rechargeDictionaryId" placeholder="请选择充值类型" style="width: 100%">
            <el-option v-for="item in rechargeDictionaryList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="套餐类型" prop="dictionaryId">
          <el-select v-model="addForm.dictionaryId" placeholder="请选择套餐类型" style="width: 100%">
            <el-option v-for="item in dictionaryCodeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="addForm.sort"></el-input-number>
        </el-form-item>

        <el-form-item label="每月最大充值数" prop="maxRechargeCount">
          <el-input-number v-model="addForm.maxRechargeCount"></el-input-number>
        </el-form-item>

        <el-form-item label="套餐数量" prop="count">
          <el-input-number v-model="addForm.count"></el-input-number>
        </el-form-item>
             
        <el-form-item label="成本价" prop="costPrice">
          <el-input-number v-model="addForm.costPrice"></el-input-number>
        </el-form-item>

        <el-form-item label="套餐金额" prop="packageAmount">
          <el-input-number v-model="addForm.packageAmount"></el-input-number>
        </el-form-item>
             
        <el-form-item label="真量（G）" prop="realFlow">
          <el-input-number v-model="addForm.realFlow"></el-input-number>
        </el-form-item>

        <el-form-item label="虚量（G）" prop="emptyFlow">
          <el-input-number v-model="addForm.emptyFlow"></el-input-number>
        </el-form-item>

        <el-form-item label="显示" prop="isShow">
          <el-switch v-model="addForm.isShow" inactive-color="#ff4949" active-color="#13ce66">
          </el-switch>
        </el-form-item>

        <el-form-item label="禁用" prop="isDisable">
          <el-switch v-model="addForm.isDisable" inactive-color="#ff4949" active-color="#13ce66">
          </el-switch>
        </el-form-item>

        <el-form-item label="热卖" prop="isHeat">
          <el-switch v-model="addForm.isHeat" inactive-color="#ff4949" active-color="#13ce66">
          </el-switch>
        </el-form-item>
             
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" placeholder="请输入备注" v-model="addForm.remark" maxlength="225" show-word-limit>
          </el-input>
        </el-form-item>

      </el-form>
      <template #footer>
        <el-button @click.native="addDialogFormVisible = false">取消</el-button>
        <my-confirm-button type="submit" :validate="addFormvalidate" :loading="addLoading" @click="onAddSubmit" />
      </template>
    </my-window>

    <!--编辑窗口-->
    <my-window v-if="checkPermission(['api:admin:package:update'])" title="编辑" :visible.sync="editFormVisible"
      @close="closeEditForm">
      <el-form ref="editForm" :model="editForm" :rules="editFormRules" label-width="130px" :inline="false">
             
        <el-form-item label="名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off" />
        </el-form-item>

        <el-form-item label="运营商" prop="carrierOperatorId">
          <el-select v-model="editForm.carrierOperatorId" placeholder="请选择运营商" style="width: 100%">
            <el-option v-for="item in carrierOperatorList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="套餐系列" prop="carrierOperatorPackageDictionaryId">
          <el-select v-model="editForm.carrierOperatorPackageDictionaryId" placeholder="请选择套餐系列" style="width: 100%">
            <el-option v-for="item in carrierOperatorPackageDictionaryList" :key="item.id" :label="item.name"
              :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="充值类型" prop="rechargeDictionaryId">
          <el-select v-model="editForm.rechargeDictionaryId" placeholder="请选择充值类型" style="width: 100%">
            <el-option v-for="item in rechargeDictionaryList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="套餐类型" prop="dictionaryId">
          <el-select v-model="editForm.dictionaryId" placeholder="请选择套餐类型" style="width: 100%">
            <el-option v-for="item in dictionaryCodeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="editForm.sort"></el-input-number>
        </el-form-item>

        <el-form-item label="每月最大充值数" prop="maxRechargeCount">
          <el-input-number v-model="editForm.maxRechargeCount"></el-input-number>
        </el-form-item>

        <el-form-item label="套餐数量" prop="count">
          <el-input-number v-model="editForm.count"></el-input-number>
        </el-form-item>
             
        <el-form-item label="成本价" prop="costPrice">
          <el-input-number v-model="editForm.costPrice"></el-input-number>
        </el-form-item>

        <el-form-item label="套餐金额" prop="packageAmount">
          <el-input-number v-model="editForm.packageAmount"></el-input-number>
        </el-form-item>
             
        <el-form-item label="真量（G）" prop="realFlow">
          <el-input-number v-model="editForm.realFlow"></el-input-number>
        </el-form-item>

        <el-form-item label="虚量（G）" prop="emptyFlow">
          <el-input-number v-model="editForm.emptyFlow"></el-input-number>
        </el-form-item>

        <el-form-item label="显示" prop="isShow">
          <el-switch v-model="editForm.isShow" inactive-color="#ff4949" active-color="#13ce66">
          </el-switch>
        </el-form-item>

        <el-form-item label="禁用" prop="isDisable">
          <el-switch v-model="editForm.isDisable" inactive-color="#ff4949" active-color="#13ce66">
          </el-switch>
        </el-form-item>

        <el-form-item label="热卖" prop="isHeat">
          <el-switch v-model="editForm.isHeat" inactive-color="#ff4949" active-color="#13ce66">
          </el-switch>
        </el-form-item>
             
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" placeholder="请输入备注" v-model="editForm.remark" maxlength="225" show-word-limit>
          </el-input>
        </el-form-item>

      </el-form>
      <template #footer>
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <my-confirm-button type="submit" :validate="editFormvalidate" :loading="editLoading" @click="onEditSubmit" />
      </template>
    </my-window>

  </my-container>
</template>

<script>
import { formatTime, listToTree, getTreeParents } from '@/utils'
import { getPageList, get, add, update, softDelete, batchSoftDelete } from '@/api/iot/package'
import { getEmptyQuantityLimitVelocity, emptyQuantityAddUpdate, limitVelocityAddUpdate } from '@/api/iot/empty-flow-speed-fictitiou'
import { getCarrierOperatorList } from '@/api/iot/carrier-operator'
import { getCodeList } from "@/api/admin/dictionary-type";
import MyContainer from '@/components/my-container'
import MyConfirmButton from '@/components/my-confirm-button'
import MySearch from '@/components/my-search'
import MySearchWindow from '@/components/my-search-window'
import MyWindow from '@/components/my-window'

export default {
  name: 'package',
  components: { MyContainer, MyConfirmButton, MySearch, MySearchWindow, MyWindow },
  data() {
    return {
      // 高级查询字段
      fields: [
        {
          value: 'creationTime', label: '创建时间', type: 'date', operator: 'daterange',
          config: { type: 'daterange', format: 'yyyy-MM-dd', valueFormat: 'yyyy-MM-dd' }
        }
      ],
      dynamicFilter: null,
      pageList: [],
      carrierOperatorList: [],
      dictionaryCodeList: [],
      carrierOperatorPackageDictionaryList: [],
      rechargeDictionaryList: [],
      networkCardLimitDictionaryList: [],
      total: 0,
      sels: [], // 列表选中列
      listLoading: false,
      pageLoading: false,
      editLoading: false,
      addLoading: false,
      addFictitiousLoading: false,
      addLimitVelocityLoading: false,
      deleteLoading: false,
      // 上传图片等待
      avatarLoading: false,
      // 添加界面
      addDialogFormVisible: false,
      // 添加界面
      addFictitiousDialogFormVisible: false,
      addLimitVelocityDialogFormVisible: false,
      // 编辑界面是否显示
      editFormVisible: false,
      editFormRules: {
        carrierOperatorId: [{ required: true, message: '请输入运营商Id', trigger: 'blur' }],
        carrierOperatorPackageDictionaryId: [{ required: true, message: '请输入套餐运营商系列', trigger: 'blur' }],
        rechargeDictionaryId: [{ required: true, message: '请输入充值类型', trigger: 'blur' }],
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        dictionaryId: [{ required: true, message: '请输入套餐类型', trigger: 'blur' }],
        isShow: [{ required: true, message: '请输入显示', trigger: 'blur' }],
        maxRechargeCount: [{ required: true, message: '请输入每月最大充值数', trigger: 'blur' }],
        count: [{ required: true, message: '请输入套餐数量', trigger: 'blur' }],
        costPrice: [{ required: true, message: '请输入成本价', trigger: 'blur' }],
        packageAmount: [{ required: true, message: '请输入套餐金额', trigger: 'blur' }],
        emptyFlow: [{ required: true, message: '请输入虚量（G）', trigger: 'blur' }],
        realFlow: [{ required: true, message: '请输入真实流量（G）', trigger: 'blur' }],
        isDisable: [{ required: true, message: '请输入禁用', trigger: 'blur' }],
        isHeat: [{ required: true, message: '请输入热卖', trigger: 'blur' }],
      },
      addFormRules: {
        carrierOperatorId: [{ required: true, message: '请输入运营商Id', trigger: 'blur' }],
        carrierOperatorPackageDictionaryId: [{ required: true, message: '请输入套餐运营商系列', trigger: 'blur' }],
        rechargeDictionaryId: [{ required: true, message: '请输入充值类型', trigger: 'blur' }],
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        dictionaryId: [{ required: true, message: '请输入套餐类型', trigger: 'blur' }],
        isShow: [{ required: true, message: '请输入显示', trigger: 'blur' }],
        maxRechargeCount: [{ required: true, message: '请输入每月最大充值数', trigger: 'blur' }],
        count: [{ required: true, message: '请输入套餐数量', trigger: 'blur' }],
        costPrice: [{ required: true, message: '请输入成本价', trigger: 'blur' }],
        packageAmount: [{ required: true, message: '请输入套餐金额', trigger: 'blur' }],
        emptyFlow: [{ required: true, message: '请输入虚量（G）', trigger: 'blur' }],
        realFlow: [{ required: true, message: '请输入真实流量（G）', trigger: 'blur' }],
        isDisable: [{ required: true, message: '请输入禁用', trigger: 'blur' }],
        isHeat: [{ required: true, message: '请输入热卖', trigger: 'blur' }],
      },
      addLimitVelocityFictitiousFormRules: {
        carrierOperatorId: [{ required: true, message: '请输入运营商Id', trigger: 'blur' }],
        carrierOperatorPackageDictionaryId: [{ required: true, message: '请输入套餐运营商系列', trigger: 'blur' }],
        rechargeDictionaryId: [{ required: true, message: '请输入充值类型', trigger: 'blur' }],
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        dictionaryId: [{ required: true, message: '请输入套餐类型', trigger: 'blur' }],
        isShow: [{ required: true, message: '请输入显示', trigger: 'blur' }],
        maxRechargeCount: [{ required: true, message: '请输入每月最大充值数', trigger: 'blur' }],
        count: [{ required: true, message: '请输入套餐数量', trigger: 'blur' }],
        costPrice: [{ required: true, message: '请输入成本价', trigger: 'blur' }],
        packageAmount: [{ required: true, message: '请输入套餐金额', trigger: 'blur' }],
        emptyFlow: [{ required: true, message: '请输入虚量（G）', trigger: 'blur' }],
        realFlow: [{ required: true, message: '请输入真实流量（G）', trigger: 'blur' }],
        isDisable: [{ required: true, message: '请输入禁用', trigger: 'blur' }],
        isHeat: [{ required: true, message: '请输入热卖', trigger: 'blur' }],
      },
      // 编辑界面数据
      editForm: {
        id: "",
        carrierOperatorId: "",
        carrierOperatorPackageDictionaryId: "",
        rechargeDictionaryId: "",
        name: "",
        sort: "",
        dictionaryId: "",
        isShow: false,
        maxRechargeCount: "",
        count: "",
        costPrice: "",
        packageAmount: "",
        emptyFlow: "",
        realFlow: "",
        isDisable: false,
        isHeat: false,
        isOpenAccount: false,
        remark: "",
      },
      // 添加界面数据
      addForm: {
        id: "",
        carrierOperatorId: "",
        carrierOperatorPackageDictionaryId: "",
        rechargeDictionaryId: "",
        name: "",
        sort: "",
        dictionaryId: "",
        isShow: false,
        maxRechargeCount: "",
        count: "",
        costPrice: "",
        packageAmount: "",
        emptyFlow: "",
        realFlow: "",
        isDisable: false,
        isHeat: false,
        isOpenAccount: false,
        remark: "",
      },
      // 添加虚量/限速数据
      addLimitVelocityFictitiousForm: {
        id: null,
        packageId: null,
        limitDictionaryId: null,
        emptyFlowFictitiousJson: [
          {
            begin: "",
            end: "",
            ratio: "",
            sort: 0,
          }
        ],
        speedFictitiousJson: [{
          begin: "",
          end: "",
          limitVelocity: "",
          sort: 0,
        }]
      }
    }
  },
  async mounted() {
    await this.onGetPageList()
    await this.onGetCodeList();
    await this.onGetCarrierOperatorList();
  },
  methods: {
    formatCreatedTime(row, column, time) {
      return formatTime(time, 'YYYY-MM-DD HH:mm')
    },
    // 查询
    onSearch(dynamicFilter) {
      this.$refs.pager.setPage(1)
      this.dynamicFilter = dynamicFilter
      this.onGetPageList()
    },
    // 显示添加界面
    onAdd() {
      this.addDialogFormVisible = true;
    },
    // 关闭添加
    closeAddForm() {
      this.$refs.addForm.resetFields()
      this.addDialogFormVisible = false
    },
    // 关闭编辑
    closeEditForm() {
      this.$refs.editForm.resetFields()
      this.editFormVisible = false
    },
    // 关闭虚量/限速配置
    closeAddLimitVelocityFictitiousForm() {
      this.$refs.addLimitVelocityFictitiousForm.resetFields()
      this.addFictitiousDialogFormVisible = false
    },
    // 添加虚量配置
    onAddEmptyFlowFictitiousJson() {
      this.addLimitVelocityFictitiousForm.emptyFlowFictitiousJson.push({
        begin: "",
        end: "",
        ratio: "",
        sort: this.addLimitVelocityFictitiousForm.emptyFlowFictitiousJson.length,
      });
    },
    onDeleteEmptyFlowFictitiousJson(sort) {
      if (sort == 0) {
        return false;
      }
      this.addLimitVelocityFictitiousForm.emptyFlowFictitiousJson.splice(sort, 1);
    },
    // 添加限速配置
    onAddLimitVelocityJson() {
      this.addLimitVelocityFictitiousForm.speedFictitiousJson.push({
        begin: "",
        end: "",
        limitVelocity: "",
        sort: this.addLimitVelocityFictitiousForm.speedFictitiousJson.length,
      });
    },
    onDeleteLimitVelocityJson(sort) {
      if (sort == 0) {
        return false;
      }
      this.addLimitVelocityFictitiousForm.speedFictitiousJson.splice(sort, 1);
    },
    // 添加验证
    addFormvalidate() {
      let isValid = false
      this.$refs.addForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    addLimitVelocityFictitiousFormvalidate() {
      let isValid = false
      this.$refs.addLimitVelocityFictitiousForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    // 编辑验证
    editFormvalidate() {
      let isValid = false
      this.$refs.editForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    // 删除验证
    deleteValidate(row) {
      let isValid = true
      return isValid
    },
    // 批量删除验证
    batchDeleteValidate() {
      let isValid = true
      return isValid
    },
    // 选择
    onSelsChange(sels) {
      this.sels = sels
    },
    // 获取列表
    async onGetPageList() {
      var pager = this.$refs.pager.getPager()
      const params = {
        ...pager,
        dynamicFilter: this.dynamicFilter
      }
      this.listLoading = true
      const res = await getPageList(params)
      this.listLoading = false
      if (!res?.success) {
        return
      }
      this.total = res.data.total
      const data = res.data.list
      data.forEach(d => {
        d._loading = false
      })
      this.pageList = data
    },
    // 获取运营商列表
    async onGetCarrierOperatorList() {
      const res = await getCarrierOperatorList();
      if (!res?.success) {
        return
      }
      this.carrierOperatorList = res.data;
    },
    // 根据Code获取字典
    async onGetCodeList() {
      const res = await getCodeList({ code: "CarrierOperatorPackageDictionaryCode" });
      if (!res?.success) {
        return;
      }
      this.carrierOperatorPackageDictionaryList = res.data;

      var packageType = await getCodeList({ code: "PackageTypeCode" });
      if (!packageType?.success) {
        return;
      }
      this.dictionaryCodeList = packageType.data;

      var rechargeType = await getCodeList({ code: "PaymentRechargeTypeCode" });
      if (!rechargeType?.success) {
        return;
      }
      this.rechargeDictionaryList = rechargeType.data;

      var networkCardLimitType = await getCodeList({ code: "NetworkCardLimitCode" });
      if (!rechargeType?.success) {
        return;
      }
      this.networkCardLimitDictionaryList = networkCardLimitType.data;
    },
    // 显示编辑界面
    async onEdit(index, row) {
      this.pageLoading = true
      const res = await get({ id: row.id })
      this.pageLoading = false
      if (res && res.success) {
        const data = res.data;
        this.editForm = data;
        this.editFormVisible = true;
      }
    },
    // 显示虚量And限速界面
    async onFictitiousEdit(index, row) {
      this.addLimitVelocityFictitiousForm.packageId = row.id;

      const res = await getEmptyQuantityLimitVelocity({ id: row.id })
      if (res && res.success && res.data) {
        const data = res.data;
        if (data.emptyFlowFictitiousList && data.emptyFlowFictitiousList.length) {
          data.emptyFlowFictitiousJson = data.emptyFlowFictitiousList;
        } else {
          data.emptyFlowFictitiousJson = [
            {
              begin: "",
              end: "",
              ratio: "",
              sort: 0,
            }
          ];
        }

        if (data.speedFictitiousList && data.speedFictitiousList.length) {
          data.speedFictitiousJson = data.speedFictitiousList;
        } else {
          data.speedFictitiousJson = [
            {
              begin: "",
              end: "",
              limitVelocity: "",
              sort: 0,
            }
          ];
        }

        this.addLimitVelocityFictitiousForm = data;
      }
      if (index == 1) {
        this.addFictitiousDialogFormVisible = true;
        return false;
      } else if (index == 2) {
        this.addLimitVelocityDialogFormVisible = true;
      }
    },
    // 添加
    async onAddSubmit() {
      this.addLoading = true;
      const para = _.cloneDeep(this.addForm);
      const res = await add(para);
      this.addLoading = false;

      if (!res?.success) {
        return;
      }
      this.$message({
        message: this.$t("admin.addOk"),
        type: "success",
      });
      this.$refs["addForm"].resetFields();
      this.addDialogFormVisible = false;
      this.onGetPageList();
    },
    async onAddFictitiousSubmit() {
      const para = _.cloneDeep(this.addLimitVelocityFictitiousForm);
      const res = await emptyQuantityAddUpdate(para);

      if (!res?.success) {
        return;
      }
      this.$message({
        message: this.$t("admin.addOk"),
        type: "success",
      });
      this.$refs["addLimitVelocityFictitiousForm"].resetFields();
      this.addFictitiousDialogFormVisible = false;
    },
    async onAddLimitVelocitySubmit() {
      const para = _.cloneDeep(this.addLimitVelocityFictitiousForm);
      const res = await limitVelocityAddUpdate(para);

      if (!res?.success) {
        return;
      }
      this.$message({
        message: this.$t("admin.addOk"),
        type: "success",
      });
      this.$refs["addLimitVelocityFictitiousForm"].resetFields();
      this.addLimitVelocityDialogFormVisible = false;
    },
    // 编辑
    async onEditSubmit() {
      this.editLoading = true
      const para = _.cloneDeep(this.editForm)
      const res = await update(para)
      this.editLoading = false

      if (!res?.success) {
        return
      }

      this.$message({
        message: this.$t('admin.updateOk'),
        type: 'success'
      })
      this.$refs['editForm'].resetFields()
      this.editFormVisible = false
      this.onGetPageList()
    },
    // 批量删除
    async onBatchDelete() {
      const para = { ids: [] };
      para.ids = this.sels.map((s) => {
        return s.id;
      });
      this.deleteLoading = true;
      const res = await batchSoftDelete(para.ids);
      this.deleteLoading = false;

      if (!res?.success) {
        return;
      }

      this.$message({
        message: this.$t("admin.batchDeleteOk"),
        type: "success",
      });

      this.onGetPageList();
    },
    // 删除
    async onDelete(index, row) {
      row._loading = true
      const para = { id: row.id }
      const res = await softDelete(para)
      row._loading = false
      if (!res?.success) {
        return
      }
      this.$message({
        message: this.$t('admin.deleteOk'),
        type: 'success'
      })
      this.onGetPageList()
    }
  }
}
</script>

<style lang="scss" scoped>
.my-search ::v-deep .el-input-group__prepend {
  background-color: #fff;
}
</style>
